package group2projman;

import java.text.SimpleDateFormat;
import java.util.Date;

public class Loan extends BookTransaction {
	Date dueDate;
	
	public Loan(int id, int bookCopyId, Type type) {
		super(id, bookCopyId, type);		
	}
	
	public Loan(int id, int bookCopyId) {
		super(id, bookCopyId, Transaction.Type.LOAN);
	}
	
	public String listAttributes() {
		StringBuffer result = new StringBuffer(super.listAttributes());
				
		result.append(",str:bookCopyId;").append(0);
		result.append(",dat:dueDate;").append(dueDate);
		result.append(",int:lateFee;").append(0);
		result.append(",int:").append("amount;").append(0);
		result.append(",STR:description;").append(0);
		
		return result.toString();
	}
	
	public boolean loadAttributes(String desc) {
		if (super.loadAttributes(desc))
			return true;
		
		try {
			String[] split = desc.split("[;,]");	
			
			if (!split[8].equals("dat:dueDate"))
				return true;
			dueDate = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy").parse(split[9]);
		}  catch (Exception e) {
			// Array index out of bounds or enum value not found
			return true;
		}
		
		return false;
	}		
}
